/*
 * Application Name : MetaData
 *
 * Project Name : iDea
 *
 * Inputs : TODO <List Program input and configuration files>
 *
 * Parameters : TODO <List command line parameters for this program>
 *
 * Dependencies : TODO <List the libraries or other applications that this program depends on>
 *
 * Outputs : TODO <List the files this program will output to>
 *
 * Description : TODO <A brief description of the program's function>
 *
 * Detailed Design : TODO <Include name and location of the detail design document>
 *
 * Program History :
 *      Author : Massimo Renke <massimo.renke@census.gov>
 *      Date : Apr 26, 2013 / 11:11:41 AM
 *      Changes : First created.
 *
 *
 * Other : TODO <Include any other information that someone who will maintain this code should know>
 *
 */
package idea.sfo.viewer.utilites;


import com.itextpdf.awt.PdfGraphics2D;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import idea.sfo.viewer.graphics.ColorSpec;
import idea.sfo.viewer.graphics.Drawable;
import java.awt.Dimension;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;


/**
 *
 * @author Massimo Renke <massimo.renke@census.gov>
 */
public class PdfFileWriter
{

    /**
     *
     * @param fileName
     * @param imageSize
     * @param graphics
     */
    public static void printPdf(
            String fileName ,
            Dimension imageSize ,
            ArrayList<Drawable> graphics )
            throws DocumentException , FileNotFoundException
    {
        //
//        Rectangle pageArea = new Rectangle( PageSize.A3 );
        Rectangle pageArea = new Rectangle( imageSize.width , imageSize.height );
        Document pdfDoc = new Document( pageArea );

        //
        PdfWriter writer = PdfWriter.getInstance(
                pdfDoc ,
                new FileOutputStream( fileName ) );

        pdfDoc.open();

        PdfGraphics2D pdfG2d = new PdfGraphics2D(
                writer.getDirectContent() ,
                imageSize.width ,
                imageSize.height );

        // write
        // set the background color.
        pdfG2d.setColor( ColorSpec.getDefaultBackgroundColor() );
        pdfG2d.fillRect( 0 , 0 , imageSize.width , imageSize.height );

        for ( Drawable graphic : graphics )
        {
            graphic.draw( pdfG2d );
        }
        pdfG2d.dispose();

        pdfDoc.close();
    }

}
